// pages/home/home.js
import points from '../../assets/json/points.js';
const app = getApp();
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    points: points,
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    user: {
      avatar: defaultAvatarUrl
    },
    hasUserInfo: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const user = wx.getStorageSync('user');
    if (user) {
      this.setData({
        user: user,
        hasUserInfo: true
      });
      return;
    } else {
      this.setData({
        hasUserInfo: false
      });
    }
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.showLoading({
          title: '加载中',
          mask: true
        });
        wx.request({
          url: app.globalData.api + '/users/wechat',
          method: 'POST',
          data: {
            code: res.code
          },
          success: (result) => {
            wx.hideLoading();
            wx.setStorageSync('openId', result.data.openId);
            this.data.user.openId = result.data.openId;
            if (result.data.bind) {
              wx.setStorageSync('user', result.data.user);
              this.setData({
                user: result.data.user,
                hasUserInfo: true
              })
            } 
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    console.log("onReady");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("onShow");
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log("onHide");
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log("onUnload");
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 跳转地图页面
   */
  navigateToMap() {
    wx.navigateTo({
      url: "../../pages/map/map"
    });
  },

  /**
   * 跳转成就页面
   */
  navigateToMe() {
    wx.navigateTo({
      url: "../../pages/me/me"
    });
  },

  navigateToMoment() {
    wx.navigateTo({
      url: "../../pages/moment/moment"
    });
  },

  scan() {
    wx.scanCode({
      success: (res) => {
        console.log(res.result);
        wx.navigateTo({
          url: '../../pages/point/point?id=' + res.result
        })
      }
    })
  },

  submit() {
    if (this.data.user.name.trim() == "") {
      wx.showToast({
        title: '请输入昵称！',
        icon: 'none'
      });
    } else {
      wx.showLoading({
        title: '加载中',
        mask: true
      });
      wx.request({
        url: app.globalData.api + '/users/bind',
        method: 'POST',
        data: {
          user: this.data.user
        },
        success: (result) => {
          wx.hideLoading();
          if (result.data.result) {
            wx.setStorageSync("user", result.data.user);
            this.setData({
              hasUserInfo: true
            });
          } else {
            wx.showToast({
              title: result.data.msg,
              icon: 'none'
            });
          }
        }
      })
    }
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    const data = wx.getFileSystemManager().readFileSync(avatarUrl, "base64");
    const base64Data = `data:image/jpg;base64,${data}`;
    this.setData({
      "user.avatar": base64Data
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value;
    this.setData({
      "user.name": nickName
    })
  }
})